package Io;

import java.io.File;
import java.util.Scanner;


/**
 * 查询并删除文件
 */
public class IoDemo8 {

    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("输入你要查询的路径:");
        String basePath = scanner.next();
        File root = new File(basePath);
        if (!root.isDirectory()) {
            System.out.println("输入的路径不合法!!");
            return;
        }
        System.out.println("输入你要删除的文件:");
        String fileName = scanner.next();
        searchFile(root,fileName);
    }

    private static void searchFile(File root, String fileName) {
        System.out.println(""+root.getAbsolutePath());
        File[] files = root.listFiles();
        if (files == null) {
            return;
        }
        for (File f: files) {
            if (f.isDirectory()) {
                searchFile(f,fileName);
            }else {
                if (f.getName().contains(fileName)) {
                    System.out.println("确定要删除"+f.getAbsolutePath()+"这个文件吗?");
                    String res = scanner.next();
                    if (res.equals("Y") || res.equals("y")) {
                        f.delete();
                        System.out.println("删除成功!");
                    }else {
                        System.out.println("删除取消!");
                    }
                }
            }
        }

    }

}
